﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Modules.ProductList
{
    public partial class ProductList : Controls.BaseUserControl
    {
        public bool DisplayOldPrice = true;
        public bool EnableSort = true;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            DisplayOldPrice = Convert.ToBoolean(ReferenceModule.GetModuleConfig("DisplayOldPrice"));
            bool IsHot = Convert.ToBoolean(ReferenceModule.GetModuleConfig("IsHot"));
            bool IsNew = Convert.ToBoolean(ReferenceModule.GetModuleConfig("IsNew"));
            int NumberOfProducts = ReferenceModule.GetModuleConfig("NumberOfProducts").ToInt32TryParse();
            int NumberOfRows = ReferenceModule.GetModuleConfig("NumberOfRows").ToInt32TryParse();
            int NumberOfColumns = ReferenceModule.GetModuleConfig("NumberOfColumns").ToInt32TryParse();
            int Category = Request.QueryString["ProductCategoryID"].ToInt32TryParse();
            if (Category == 0) { Category = ReferenceModule.GetModuleConfig("Category").ToInt32TryParse(); }
            EnableSort = Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnabledSort"));
            if (!EnableSort)
            {
                ProductSearch1.EnableQuickView = SortContainer.Visible = false;
                SortContainer.Dispose();
                ProductSearch1.Dispose();
            }
            List<Product> source;
            if (Category > 0)
            {
                this.ltrTitle.Text = ProductCategoryBLL.GetByID((int)Category).Name;
                source = ProductBLL.GetByCategory(Category).ToList();
            }
            else
            {
                this.ltrTitle.Text = ReferenceModule.GetModuleConfig("Header").ToString();
                string keyword = Request.QueryString["key"];
                if (!string.IsNullOrEmpty(keyword))
                {
                    source = ProductBLL.FulltextSearch(keyword);
                }
                else
                {
                    source = ProductBLL.GetAll().ToList();
                }
            }
            //Check Filter
            if (!string.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                source = source.FilterByProperties(GetFilters());
            }
            //End Filter

            Pager1.PageSize = NumberOfColumns * NumberOfRows;
            dtlProduct.RepeatColumns = NumberOfColumns;
            string sort = Request.QueryString["sort"];
            string dir = Request.QueryString["dir"];
            List<string> conditions = GetConditions();
            conditions.Insert(0, "1-True-IsEnabled-eq");
            if (IsHot) conditions.Insert(0, "1-True-IsHot-eq");
            if (IsNew) conditions.Insert(0, "1-True-IsNew-eq");

            Pager1.DataSource = source.DataProcessing(conditions: conditions, sort: string.IsNullOrEmpty(sort) ? "SortOrder" : sort, dir: string.IsNullOrEmpty(dir) ? "desc" : dir, thenby: "ProductID", thenbyDir: "desc", start: NumberOfProducts > 0 ? 0 : -1, limit: NumberOfProducts > 0 ? NumberOfProducts : -1);
            Pager1.BindToControl = dtlProduct;
            dtlProduct.DataSource = Pager1.DataSourcePaged;
            if (DDLSort.Items.FindByValue(sort) != null) DDLSort.SelectedValue = sort;
            if (DDLDir.Items.FindByValue(dir) != null) DDLDir.SelectedValue = dir;
            base.OnInit(e);

        }

        protected void ChangeSort(object sender, EventArgs e)
        {
            Response.Redirect(URLUtils.AddParamToQueryString(new Dictionary<string, string>() { { "sort", DDLSort.SelectedValue }, { "dir", DDLDir.SelectedValue } }));
        }

        protected List<string> GetConditions()
        {
            List<string> conditions = new List<string>();
            if (!string.IsNullOrEmpty(Request.QueryString["c"]))
            {
                conditions = (Request.QueryString["c"] + ";").Split(';').ToList().Where(str => !string.IsNullOrEmpty(str)).ToList();
            }
            return conditions;
        }

        protected List<string> GetFilters()
        {
            List<string> filters = new List<string>();
            if (!string.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                filters = (Request.QueryString["filter"] + ";").Split(';').ToList().Where(str => !string.IsNullOrEmpty(str)).ToList();
            }
            return filters;
        }

    }

}